/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-25
 * @time 下午6:42:38
 */
package com.ssh.dao;

import java.sql.Timestamp;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.ssh.entity.InnerVehicle;
import com.ssh.model.pdata.InnerVehicleUseInfo;

/**
 * @author jack
 *
 */
public class InnerVehicleDAOImpl implements InnerVehicleDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.InnerVehicleDAOI#add(com.ssh.entity.InnerVehicle)
	 */
	@Override
	public InnerVehicle add(InnerVehicle innerVehicle) {
		// TODO Auto-generated method stub
		try {
			return (InnerVehicle) sessionFactory.getCurrentSession().save(
					innerVehicle);
		} catch (Exception e) {
			return null;
		}

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.InnerVehicleDAOI#edit(com.ssh.entity.InnerVehicle)
	 */
	@Override
	public InnerVehicle edit(InnerVehicle innerVehicle) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().update(innerVehicle);
			return innerVehicle;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.InnerVehicleDAOI#view(com.ssh.entity.InnerVehicle)
	 */
	@Override
	public InnerVehicle view(InnerVehicle innerVehicle) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i.vehicleId = ? ";
			return (InnerVehicle) sessionFactory.getCurrentSession()
					.createQuery(hql)
					.setParameter(0, innerVehicle.getVehicleId())
					.uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.InnerVehicleDAOI#viewById(java.lang.String)
	 */
	@Override
	public InnerVehicle viewById(String innerVehicleId) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i.vehicleId = ? ";
			return (InnerVehicle) sessionFactory.getCurrentSession()
					.createQuery(hql).setParameter(0, innerVehicleId)
					.uniqueResult();
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.InnerVehicleDAOI#viewVehicleList(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<InnerVehicle> viewVehicleList(String param) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i."+param+" = :param";
			return sessionFactory.getCurrentSession().createQuery(hql)
					.setParameter("param", param).list();
		} catch (Exception e) {
			return null;
		}

	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.InnerVehicleDAOI#delete(com.ssh.entity.InnerVehicle)
	 */
	@Override
	public int delete(InnerVehicle innerVehicle) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().delete(innerVehicle);
			return 1;
		}
		catch(Exception e){
			e.printStackTrace();
			return 0;
		}
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.InnerVehicleDAOI#deleteById(java.lang.String)
	 */
	@Override
	public int deleteById(String innerVehicleId) {
		// TODO Auto-generated method stub
		try{
			String hql ="delete from InnerVehicle as i where i.vehicleId=?";
			return sessionFactory.getCurrentSession().createQuery(hql).
			   setParameter(0, innerVehicleId).executeUpdate();
		}
		catch(Exception e){
			return 0;
		}

	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerVehicleDAOI#viewByType(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<InnerVehicle> viewByType(String type) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i.type = :type ";
			return sessionFactory.getCurrentSession()
					.createQuery(hql).setParameter("type", type).list();
		} catch (Exception e) {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerVehicleDAOI#viewByState(java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<InnerVehicle> viewByState(String state) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i.state = :state ";
			return sessionFactory.getCurrentSession()
					.createQuery(hql).setParameter("state", state).list();
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InnerVehicle> viewByLeavelState(String leavel,String state) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i.state = :state and i.vehicleLevel=:vehicleLevel";
			return sessionFactory.getCurrentSession()
					.createQuery(hql).setParameter("state", state).setParameter("vehicleLevel", leavel).list();
		} catch (Exception e) {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerVehicleDAOI#viewVByStateType(java.lang.String, java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<InnerVehicle> viewVByStateType(String state, String type) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i.state = :state and i.type = :type";
			return sessionFactory.getCurrentSession()
					.createQuery(hql).setParameter("state", state).
					setParameter("type", type).list();
		} catch (Exception e) {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerVehicleDAOI#getAll()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<InnerVehicle> getAll() {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle";
			return sessionFactory.getCurrentSession().createQuery(hql).list();
		} catch (Exception e) {
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InnerVehicle> queryNUseByLevel(String level) {
		// TODO Auto-generated method stub
		try {
			String hql = "from InnerVehicle as i where i.vehicleLevel=:level";
			Query query = sessionFactory.getCurrentSession().createQuery(hql) ;
			query.setParameter("level", level);
			return query.list();
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public InnerVehicle update(InnerVehicle innerVehicle) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().update(innerVehicle);
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
		return innerVehicle;
	}
	/**按车牌号查询 出入车辆信息
	*/
	@Override
	public List<InnerVehicleUseInfo> queryVUseInfo(String vehicleId) {
		// TODO Auto-generated method stub
		try{
			String hql="select new com.ssh.model.pdata.InnerVehicleUseInfo() from StaffApplyCar sa,StaffUseCar su " +
					" where sa.staffId=su.staffId and su.usecarStaffid=usercarStaffid and su.vehicleId like:vehicleId";
			Query query=sessionFactory.getCurrentSession().createQuery(hql).setParameter("vehicleId",  "%"+vehicleId+"%");
			return  query.list();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

	/**查询 出入车辆使用信息
	*/
	@Override
	public List<InnerVehicleUseInfo> queryAllVUseInfo() {
		// TODO Auto-generated method stub
		try{
			String hql="select new com.ssh.model.pdata.InnerVehicleUseInfo(sa,su) from StaffApplyCar sa,StaffUseCar su" +
					" where sa.staffId=su.staffId and su.usecarStaffid=sa.usercarStaffid and sa.applyResult=:state" +
					" and sa.applyDate=su.applyDate";
			Query query=sessionFactory.getCurrentSession().createQuery(hql).setParameter("state", "agree");
			return  query.list();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}

/**按出入时间查询 出入车辆信息
	*/
	@Override
	public List<InnerVehicleUseInfo>queryVUseInfo(Timestamp startTime,
			Timestamp endTime) {
		// TODO Auto-generated method stub
		String hql="";
		long inTime_end = startTime.getTime()+15*60*1000;
		long inTime_start = startTime.getTime()-15*60*1000;
		long offTime_end = endTime.getTime()+15*60*1000;
		long offTime_start = endTime.getTime()-15*60*1000;

		Timestamp inTime_starts=new Timestamp(0),inTime_ends = new Timestamp(0);
		Timestamp offTime_starts=new Timestamp(0),offTime_ends = new Timestamp(0);

		inTime_starts.setTime(inTime_start);
		inTime_ends.setTime(inTime_end);
		offTime_starts.setTime(offTime_start);
		offTime_ends.setTime(offTime_end);

		try{
			 hql="select new com.ssh.model.pdata.InnerVehicleUseInfo(sa,su) " +
			 		"from StaffApplyCar sa,StaffUseCar su " +
			 		" where (sa.staffId=su.staffId and su.usecarStaffid=sa.usercarStaffid and sa.applyDate=su.applyDate and sa.applyResult=:state) " +
			 		" and su.inTime between :inTime_starts and :inTime_ends " +
			 		" and su.offTime between :offTime_starts and :offTime_ends";
			Query query=sessionFactory.getCurrentSession().createQuery(hql).setParameter("inTime_starts", inTime_starts)
					.setParameter("inTime_ends", inTime_ends).setParameter("offTime_starts", offTime_starts).
					setParameter("offTime_ends", offTime_ends).setParameter("state", "agree");
			return  query.list();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	/**按出入时间查询 出入车辆信息
	*/
	@Override
	public List<InnerVehicleUseInfo>queryVUseInfo(Timestamp time) {
		// TODO Auto-generated method stub
		String hql="";
		long time_end = time.getTime()+15*60*1000;
		long time_start = time.getTime()-15*60*1000;

		Timestamp time_starts=new Timestamp(0),time_ends = new Timestamp(0);

		time_starts.setTime(time_start);
		time_ends.setTime(time_end);
		try{
			 hql="select new com.ssh.model.pdata.InnerVehicleUseInfo(sa,su) " +
			 		"from StaffApplyCar sa,StaffUseCar su " +
			 		" where (sa.staffId=su.staffId and su.usecarStaffid=sa.usercarStaffid and sa.applyDate=su.applyDate and sa.applyResult=:state) " +
			 		" and (su.inTime between :time_starts and :time_ends or su.offTime between :time_starts and :time_ends)";
			Query query=sessionFactory.getCurrentSession().createQuery(hql).setParameter("time_starts", time_starts)
					.setParameter("time_ends", time_ends).setParameter("state", "agree");
			return  query.list();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
}
